package acwing.图.最小生成树;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Kruskal算法求最小生成树
 *
 * @author yang
 * @date 2022/1/13
 */
public class Kruskal算法求最小生成树 {

    static int M = 200010;
    static int N = 100010;
    static int[][] g = new int[M][3];
    static int INF = 0x3f3f3f3f;
    static int n;
    static int m;
    static int[] p = new int[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();

        for(int i = 1; i <= n;i ++) p[i] = i;
        for (int i = 0; i < m; i++) {
            g[i][0] = sc.nextInt();
            g[i][1] = sc.nextInt();
            g[i][2] = sc.nextInt();

        }
        int t = kruskal();
        if(t == INF) System.out.println("impossible");
        else System.out.println(t);
    }

    private static int kruskal() {
        int res = 0;
        int cnt = 0;
        Arrays.sort(g,0, m, (x,y) -> x[2] - y[2]);
        for (int i = 0; i < m; i++) {
            int a = g[i][0];
            int b = g[i][1];
            int w = g[i][2];
            a = find(a);
            b = find(b);
            if (a != b) {
                p[a] = b;
                res += w;
                cnt += 1;
            }
        }
        if (cnt < n - 1) return INF;
        return res;

    }

    private static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}
